package com.itcast.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.health.pojo.CheckItem;
import com.itcast.health.response.constant.CodeConstant;
import com.itcast.health.response.constant.MessageConstant;
import com.itcast.health.response.entity.PageResult;
import com.itcast.health.response.entity.Result;
import com.itcast.health.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    @GetMapping
    public Result findAll(){
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.QUERY_CHECKITEM_SUCCESS,true,checkItems);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id")Integer id){
        CheckItem checkItem = checkItemService.findOne(id);
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.QUERY_CHECKITEM_SUCCESS,true,checkItem);
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        CheckItem item = checkItemService.add(checkItem);
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.QUERY_CHECKITEM_SUCCESS,true,item.getId());
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        checkItemService.delete(id);
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.DELETE_CHECKITEM_SUCCESS,true);
    }

    @PutMapping("/{id}")
    public Result edit(@PathVariable("id")Integer id,@RequestBody CheckItem checkItem){
        checkItemService.update(id,checkItem);
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.EDIT_CHECKITEM_SUCCESS,true);
    }

    @RequestMapping("/{pageSize}/{currentPage}")
    public Result search(@PathVariable("pageSize")Integer pageSize,@PathVariable("currentPage") Integer currentPage,@RequestBody CheckItem checkItem){
        PageResult<CheckItem> search = checkItemService.search(checkItem, pageSize, currentPage);
        return new Result(CodeConstant.CHECKITEM_CODE_SUCCESS, MessageConstant.QUERY_CHECKITEM_SUCCESS,true,search);
    }
}
